<?php
namespace app\home\controller;
use \think\Validate;
use \think\captcha\Captcha;
use think\Session;

class Cart extends Common
{
    public function cart()
    {
        $user_id = session("user_id");
        $cart_info = db('cart')->where("user_id = {$user_id}")->find();
        $jsonStr = $cart_info['json_str'];
        $cart_arr = json_decode($jsonStr,true);
        $temp_arr = [];
        if(!$cart_arr){
            $this->assign("is_have",0);
        }else{
            foreach($cart_arr as $key=>$val){
                $food_info = db("food")->where("food_id = '{$key}'")->find();
                $food_info['num'] = $val;
                array_push($temp_arr,$food_info);
            }
            //echo "<pre>";
            //var_dump($temp_arr);die;
            $count = count($temp_arr);
            $this->assign("temp_arr",$temp_arr);
            $this->assign("count",$count);
            $this->assign("is_have",1);
        }
        $this->assign("typeaa","cart");
        $this->assign("user_id",$user_id);
    	return view('cart');
    }

    public function addCartAction(){
    	$user_id = session("user_id");
        $jsonStr = input("post.data");
        $ret = db("cart")->where("user_id = {$user_id}")->find();
        if($ret){
            $new_arr = json_decode($jsonStr,true);
            $before_arr = json_decode($ret['json_str'],true);
            foreach($new_arr as $key => &$val){
                if(array_key_exists($key,$before_arr)){ //新加入的餐品存在于原来的购物车
                    $before_arr[$key] = (string)($before_arr[$key]+$val);
                }else{                          //新加入的餐品不在原来的购物车
                    $before_arr[$key] = $val;
                }
            }
            $jsonStr = json_encode($before_arr);
            $ret2 = db("cart")->where("user_id = {$user_id}")->update(array("json_str" => $jsonStr));
            if($ret2){
                return ['status' => 1];
            }else{
                return ['status' => 0];
            }
        }else{
            $data = [
                'json_str'  => $jsonStr,
                'user_id'   => $user_id,
                'addtime'   => time()
            ];
            $ret = db('cart')->insert($data);
            if($ret){
                return ['status' => 1];
            }else{
                return ['status' => 0];
            }
        }
    }

    public function changeCart(){
    	$user_id = session("user_id");
        $data = $_POST['data'];
        $food_id = $data['food_id'];
        $num = $data['num'];
        $ret = db("cart")->where("user_id = {$user_id}")->find();
        $jsonStr = $ret['json_str'];
        $cart_arr = json_decode($jsonStr,true);
        $cart_arr[$food_id] = $num;
        $new_jsonStr = json_encode($cart_arr);
        $ret2 = db("cart")->where("user_id = {$user_id}")->update(array("json_str" => $new_jsonStr));
        if($ret2){
            return ['status' => 1];
        }else{
            return ['status' => 0];
        }
    }

    public function delCartFood(){
    	$user_id = session("user_id");
        $arr = $_POST['arr'];
        $json_str = db("cart")->where("user_id ={$user_id}")->value("json_str");
        $temp_arr = json_decode($json_str,true);
        foreach($arr as $val){
            if(array_key_exists($val,$temp_arr)){
                unset($temp_arr[$val]);
                continue;
            }
        }
        $new_jsonStr = json_encode($temp_arr);
        $ret2 = db("cart")->where("user_id = {$user_id}")->update(array("json_str" => $new_jsonStr));
        if($ret2){
            return ['status' => 1];
        }else{
            return ['status' => 0];
        }
    }
}
